﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class exame2_boom : MonoBehaviour
{
    public float boomRadius = 30f;
    public int explosionDamage = 50;
    public GameObject expFx;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnCollisionEnter(Collision collision)
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, boomRadius);
        for (int i = 0; i < colliders.Length; i++)
        {
            //计算从爆炸中心到碰撞体表面的距离
            //返回碰撞体与爆炸中心最近的点
            Vector3 closestPoint = colliders[i].ClosestPointOnBounds(transform.position);
            //计算距离
            float distance = Vector3.Distance(closestPoint, transform.position);
            //计算伤害点数的百分比，随着距离的增大降低(Clamp01 钳制数值在0~1之间  )
            float hitPointPercentage = 1.0f - Mathf.Clamp01(distance / boomRadius);
            //计算最后总的伤害点数
            int damage = (int)(hitPointPercentage * explosionDamage);
            //向受到影响的碰撞体发送伤害消息，调用该物体的减血方法
            colliders[i].SendMessageUpwards("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);
        }
        Destroy(Instantiate(expFx, transform.position, transform.rotation), 3f);
        Destroy(gameObject);
    }
}
